package com.pen.changgou.common.core.entity;

import lombok.Data;

/**
 * @param <T>
 */
@Data
public class Result<T> {
    private boolean flag;//是否成功
    private Integer code;//返回码
    private String message;//返回消息
    private T data;//返回数据

    public Result(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = (T) data;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.message = "操作成功!";
    }

    public Result(T data) {
        this.data = data;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> Result = new Result<T>(data);
        Result.setCode(0);//操作成功
        Result.setMessage("success");
        return Result;
    }

    public static <T> Result<T> ok() {
        Result<T> Result = new Result<T>();
        Result.setCode(0);//操作成功
        Result.setMessage("success");
        return Result;
    }


    public static <T> Result<T> fail(T data) {
        Result<T> Result = new Result<T>();
        Result.setCode(1);//操作失败
        Result.setMessage("fail");
        return Result;
    }


    public Result<T> msg(String msg) {
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }
}
